//
//  LoginView.m
//  template
//
//  Created by SoXun on 2018/7/17.
//  Copyright © 2018年 soxun. All rights reserved.
//

#import "LoginView.h"
//#import "WXApi.h"

#import "WXAuth.h"
#import "NSString+Category.h"
#import "YYModel.h"
#import "UserModel.h"
#import "BRAPPViewModel.h"
#import "UserModel.h"
#import "UIButton+Category.h"
#import "UIColor+BRCategory.h"
#import "UILabel+BRFont.h"
#import "SVProgressHUD.h"
#import "UIView+BRCategory.h"
//#import "WeiXinManager.h"
#import "LBBoundingIponeVC.h"
#import "LBTabBarViewController.h"
typedef void (^LoginBlock)(BOOL isOk);
@interface LoginView()<UITextFieldDelegate>
@property (nonatomic,strong)UIImageView * BackgroundImage;

@property (nonatomic,strong)UIButton * PasswordLogin;
@property (nonatomic,strong)UIButton * NoteLogin;
@property (nonatomic,strong)UIView * LineView;

@property (nonatomic,strong)UITextField * PhoneNumTF;
@property (nonatomic,strong)UITextField * PassWordTF;
@property (nonatomic,strong)UITextField * CodeTF;
@property (nonatomic,strong)UIView * LineView2;
@property (nonatomic,strong)UIButton * WeChatLogin;
@property (nonatomic,strong)UIView * NoteLoginView;

@property (nonatomic,strong)UIButton * ShowPassWordButton;
@property (nonatomic,strong)UIButton * CleanButton;
@property (nonatomic,strong)NSString * authId;
// 0 账号密码  1 短信    2 微信
@property (nonatomic,assign)NSInteger loginType;
@property (nonatomic,copy)NSString * openID;
@property (nonatomic,copy)LoginBlock  block;
@property (nonatomic,strong)UIImageView * logo;


@end

@implementation LoginView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self  SetUI];
    }
    return self;
}


- (void)NoteLogin:(UIButton *)btn{
    self.loginType = 0;
    
    self.LineView.hidden = YES;
    self.LineView2.hidden = NO;
    self.NoteLoginView.hidden = YES;
    self.PassWordTF.hidden = NO;
    self.PassWordTF.placeholder = @"请输入密码";
    self.ShowPassWordButton.hidden = YES;
    self.ForgetPassWordBtn.hidden = NO;
    [self.PasswordLogin setTitleColor:rgb(40, 40, 40) forState:(UIControlStateNormal)];
    [self.NoteLogin setTitleColor:rgb(233, 84, 18) forState:(UIControlStateNormal)];
    [self.PhoneNumTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    [self.PassWordTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    [self.PhoneNumTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [self.PassWordTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.PassWordTF.textColor = rgb(40, 40, 40);
    self.PhoneNumTF.textColor = rgb(40, 40, 40);
}
- (void)SetUI{
    
    
    self.logo = [[UIImageView alloc] init];
    [self addSubview:self.logo];
    [self.logo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(100);
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.centerX.mas_equalTo(self);
    }];
    self.logo.image = [UIImage imageNamed:@"logo"];
    //密码登录按钮
    self.PasswordLogin = [UIButton buttonWithType:(UIButtonTypeCustom)];
    //短息登录按钮
    self.NoteLogin = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    [self addSubview:self.PasswordLogin];
    [self.PasswordLogin setTitle:@"手机号登录" forState:(UIControlStateNormal)];
    [self.NoteLogin setTitle:@"账号密码登录" forState:(UIControlStateNormal)];
    self.NoteLogin.titleLabel.font = LBFont(17);
    [self.NoteLogin setTitleColor:rgb(40, 40, 40) forState:(UIControlStateNormal)];
    [self.PasswordLogin setTitleColor:rgb(233, 84, 18) forState:(UIControlStateNormal)];
    
    self.PasswordLogin.titleLabel.font = LBFont(17);
    self.loginType = 1;
    //密码登录点击事件
    [[self.PasswordLogin rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        self.loginType = 1;
        self.LineView2.hidden = YES;
        self.LineView.hidden = NO;
        self.NoteLoginView.hidden = NO;
        self.PassWordTF.hidden = YES;
         self.ForgetPassWordBtn.hidden = YES;
        [self.PasswordLogin setTitleColor:rgb(233, 84, 18) forState:(UIControlStateNormal)];
        [self.NoteLogin setTitleColor:rgb(40, 40, 40) forState:(UIControlStateNormal)];
    }];
    
    //密码按钮布局
    [self.PasswordLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.logo.mas_bottom).offset(41);
        make.width.mas_equalTo(120*kScaleFit);
        make.height.mas_equalTo(30);
    }];
    
    //左右滑动的线
    self.LineView = [[UIView alloc] init];
    [self addSubview:self.LineView];
    self.LineView.backgroundColor = rgb(233, 84, 18);
    [self.LineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.PasswordLogin);
        make.top.equalTo(self.PasswordLogin.mas_bottom).offset(0);
        make.height.mas_equalTo(2);
        make.width.equalTo(self.PasswordLogin).offset(-20);
     
    }];
    
    //中间线
    UIView * tempLineView = [[UIView alloc] init];
//    tempLineView.hidden = YES;
    [self addSubview:tempLineView];
    tempLineView.backgroundColor = [UIColor clearColor];
    [tempLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.PasswordLogin);
        make.height.equalTo(self.PasswordLogin).offset(-8);
        make.width.mas_equalTo(1);
        make.centerX.equalTo(self); make.left.equalTo(self.PasswordLogin.mas_right).offset(LBSetX(60));

    }];
    [self addSubview:self.NoteLogin];
    
    self.LineView2 = [[UIView alloc] init];
    [self addSubview:self.LineView2];
    self.LineView2.backgroundColor = self.LineView.backgroundColor;
    self.LineView2.hidden = YES;
    
    //短息按钮
    [self.NoteLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.width.equalTo(self.PasswordLogin);
        make.left.equalTo(tempLineView).offset(LBSetX(60));
    }];
    [self.LineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.NoteLogin);
        make.width.height.top.equalTo(self.LineView);
    }];
 
    [self.NoteLogin addTarget:self action:@selector(NoteLogin:) forControlEvents:(UIControlEventTouchUpInside)];
    self.PhoneNumTF = [[UITextField alloc] init];
    self.PassWordTF = [[UITextField alloc] init];
    [self addSubview:self.PhoneNumTF];
    [self addSubview:self.PassWordTF];
    self.PhoneNumTF.keyboardType = UIKeyboardTypeNumberPad;
    self.PassWordTF.secureTextEntry = YES;
    self.PhoneNumTF.placeholder = @"请输入手机号";
//    self.PassWordTF.placeholder = @"请输入密码";
    
    [self.PhoneNumTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    [self.PassWordTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];

    
    [self.PhoneNumTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [self.PassWordTF setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];

    self.PassWordTF.textColor = rgb(40, 40, 40);
    self.PhoneNumTF.textColor = rgb(40, 40, 40);
    
    UIButton * CleanButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.CleanButton = CleanButton;
    CleanButton.hidden = YES;
    [CleanButton setBackgroundImage:[UIImage imageNamed:@"food_list_close"] forState:(UIControlStateNormal)];
    [self addSubview:CleanButton];
    
    [self.PhoneNumTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.top.equalTo(self.PasswordLogin.mas_bottom).offset(LBSetY(65));
        make.height.mas_equalTo(45);
    }];
    [CleanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.PhoneNumTF.mas_right);
        make.centerY.equalTo(self.PhoneNumTF);
        make.width.height.mas_equalTo(20);
    }];
    [CleanButton addTarget:self action:@selector(CleanTF:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView * tempLineView2 = [[UIView alloc] init];
    tempLineView2.backgroundColor = rgb(210, 210, 210);
    [self addSubview:tempLineView2];
    [tempLineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.PhoneNumTF);
        make.height.equalTo(@1);
        make.right.equalTo(self).offset(-15);
        
        make.top.equalTo(self.PhoneNumTF.mas_bottom).offset(0);
    }];
    
    UIButton * ShowPassWordButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [ShowPassWordButton setBackgroundImage:[UIImage imageNamed:@"dislayLogin"] forState:(UIControlStateNormal)];
    [ShowPassWordButton setBackgroundImage:[UIImage imageNamed:@"displaynoLogin"] forState:(UIControlStateSelected)];
    [ShowPassWordButton addTarget:self action:@selector(ShowPassWord:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:ShowPassWordButton];
    ShowPassWordButton.hidden = YES;
    
    self.ShowPassWordButton = ShowPassWordButton;
    [self.PassWordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.PhoneNumTF);
        make.top.equalTo(self.PhoneNumTF.mas_bottom).offset(10);
    }];
    [ShowPassWordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(CleanButton);
        make.width.height.equalTo(CleanButton).offset(2);
        make.centerY.equalTo(self.PassWordTF);
    }];
   
    UIView * tempLine3 = [[UIView alloc] init];
    tempLine3.backgroundColor = tempLineView2.backgroundColor;
    [self addSubview:tempLine3];
    [tempLine3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(tempLineView2);
        make.top.equalTo(self.PassWordTF.mas_bottom);
    }];
    
    //登录
    self.LoginButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.LoginButton setTitle:@"登录" forState:(UIControlStateNormal)];
    self.LoginButton.titleLabel.isAdaptiveFont = @"";
    self.LoginButton.titleLabel.font = LBFont(15);
    self.LoginButton.layer.cornerRadius = 22;
    self.LoginButton.layer.masksToBounds = YES;
    [self addSubview:self.LoginButton];
    [self.LoginButton setBackgroundImage:LBImage(@"wk_shadow_btn") forState:(UIControlStateNormal)];
    [self.LoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.right.equalTo(-15);
        make.top.equalTo(self.PassWordTF.mas_bottom).offset(LBSetY(50));
        make.height.mas_equalTo(44);
    }];
    
    //注册
    self.RegisterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.RegisterButton];
    [self.RegisterButton setTitle:@"没账号去注册" forState:(UIControlStateNormal)];
    [self.RegisterButton setTitleColor: rgb(230, 84, 0) forState:(UIControlStateNormal)];
      self.RegisterButton.titleLabel.isAdaptiveFont = @"";
    self.RegisterButton.titleLabel.font = [UIFont systemFontOfSize:13*ScaleX];

    [self.RegisterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.LoginButton).offset(-4*ScaleX);
        make.top.equalTo(self.LoginButton.mas_bottom).offset(0);
        make.width.mas_equalTo(100*ScaleX);
        make.height.mas_equalTo(30*ScaleX);
    }];
    
    //忘记密码
    self.ForgetPassWordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.ForgetPassWordBtn];
    [self.ForgetPassWordBtn setTitle:@"忘记密码" forState:(UIControlStateNormal)];
    [self.ForgetPassWordBtn setTitleColor: rgb(230, 84, 0) forState:(UIControlStateNormal)];
    self.ForgetPassWordBtn.titleLabel.isAdaptiveFont = @"";
    self.ForgetPassWordBtn.titleLabel.font = [UIFont systemFontOfSize:13*ScaleX];
    self.ForgetPassWordBtn.hidden = YES;
    [self.ForgetPassWordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.LoginButton).offset(4*ScaleX);
        make.top.equalTo(self.LoginButton.mas_bottom).offset(0);
        make.width.mas_equalTo(75*ScaleX);
        make.height.mas_equalTo(30*ScaleX);
    }];

    

    //微信授权登录
    self.WeChatLogin = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self addSubview:self.WeChatLogin];
    [self.WeChatLogin setBackgroundImage:[UIImage imageNamed:@"wechat"] forState:(UIControlStateNormal)];

//    CGFloat height = LBSetY(30*ScaleX);
    self.WeChatLogin.layer.cornerRadius = 15*ScaleX;
    self.WeChatLogin.layer.masksToBounds = YES;
    [self.WeChatLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.height.mas_equalTo(45*ScaleX);
        make.bottom.mas_equalTo(-60*ScaleX);
    }];
    
    
    UILabel * tempLabel = [[UILabel alloc] init];
    tempLabel.text = @"其他登录方式";
    tempLabel.isAdaptiveFont = @"27";
    tempLabel.font = LBFont(13*ScaleX);
    tempLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:tempLabel];
    tempLabel.textColor = rgb(101, 101, 101);
    [tempLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.mas_equalTo(self.WeChatLogin).mas_equalTo(-55*ScaleX);
    }];
    
    //游客登录
    UIButton *VisitorBtn = [[UIButton alloc] init];
    self.VisitorButton = VisitorBtn;
    [VisitorBtn setTitle:@"游客登录" forState:UIControlStateNormal];

    [VisitorBtn setTitleColor:rgb(101, 101, 101) forState:UIControlStateNormal];
    VisitorBtn.titleLabel.font = [UIFont systemFontOfSize:13*ScaleX];
    [self addSubview:VisitorBtn];
    [VisitorBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(tempLabel).mas_equalTo(-40*ScaleX);
        make.size.mas_equalTo(CGSizeMake(80*ScaleX, 25*ScaleX));
    }];
    
    //其他登录方式 左边的线
    UIView * TempLineView3 = [[UIView alloc] init];
    TempLineView3.backgroundColor = rgb(152, 152, 152);
    [self addSubview:TempLineView3];
    [TempLineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(tempLabel.mas_left).mas_equalTo(-10);
        make.width.equalTo(40);
        make.centerY.mas_equalTo(tempLabel);
        make.height.mas_equalTo(1);
    }];
    
    //右边线
    UIView * TempLineView4 = [[UIView alloc] init];
    TempLineView4.backgroundColor = TempLineView3.backgroundColor;
    [self addSubview:TempLineView4];
    [TempLineView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tempLabel.mas_right).mas_equalTo(10);
        make.width.equalTo(40);
        make.centerY.mas_equalTo(tempLabel);
        make.height.mas_equalTo(1);
    }];

    if ([WXApi isWXAppInstalled]) {
        
        tempLabel.hidden = NO;
        self.WeChatLogin.hidden = NO;
        TempLineView3.hidden = NO;
        TempLineView4.hidden = NO;
    }else{
        
        tempLabel.hidden = YES;
        self.WeChatLogin.hidden = YES;
        TempLineView3.hidden = YES;
        TempLineView4.hidden = YES;
    }
    [self.WeChatLogin addTarget:self action:@selector(WeChatLoginAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.NoteLoginView = [self SetNoteLogin];
    //显示验证码
    self.NoteLoginView.hidden = NO;
    self.PhoneNumTF.delegate = self;
    self.PassWordTF.delegate = self;
    self.CodeTF.delegate = self;
    
   
}

- (void)LoginVisitorAction:(LoginBlock)block{
    self.block = block;
   NSString *uuid =  [UUIDTool getUUIDInKeychain];


    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setObject:uuid forKey:@"uuid"];
    [WYHXNetWorkManager post:WKVisitorLogin_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        NSString *msg = responseObj[@"msg"];
        if ([code integerValue]==200) {
            //游客登录后保存信息
            NSDictionary *dict = (NSDictionary *)responseObj;
            NSDictionary *data = dict[@"data"];
            UserModel *model = [UserModel mj_objectWithKeyValues:data];
            [UserModel SaveUserModel:model];
            NSString *userMoney = data[@"userMoney"];
            NSString *isVisitor = data[@"isVisitor"];
            [[NSUserDefaults standardUserDefaults] setObject:userMoney forKey:@"userMoney"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [[NSUserDefaults standardUserDefaults] setObject:isVisitor forKey:KDeviceUUID];
            [[NSUserDefaults standardUserDefaults] synchronize];
            block(YES);
        }else{
            [MBProgressHUD showSuccess:msg toView:self];
        }
    }];
}
- (void)clickRegisterBtn:(UIButton *)sender{
    
}
- (void)clickWeixinChat{
    
}


- (void)CleanTF:(UIButton *)btn{
    self.PhoneNumTF.text = @"";
}

- (void)ShowPassWord:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.PassWordTF.secureTextEntry = !btn.selected;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.PhoneNumTF.text.length > 0) {
            self.CleanButton.hidden = NO;
        }else{
            self.CleanButton.hidden = YES;
        }
        if (self.PassWordTF.text.length > 0 && self.loginType == 0) {
            self.ShowPassWordButton.hidden = NO;
        }
        if (self.loginType == 0) {
            if (self.PhoneNumTF.text.length >0 && self.PassWordTF.text.length > 0) {
//                [self.LoginButton setBackgro1undImage:LBImage(@"login_button") forState:(UIControlStateNormal)];
            }else{
//                [self.LoginButton setBackgroundImage:LBImage(@"login_button_dis") forState:(UIControlStateNormal)];
            }
        }
        if (self.loginType == 1) {
            if (self.PhoneNumTF.text.length >0 && self.CodeTF.text.length > 0 ) {
//                [self.LoginButton setBackgroundImage:LBImage(@"login_button") forState:(UIControlStateNormal)];
            }else{
//                [self.LoginButton setBackgroundImage:LBImage(@"login_button_dis") forState:(UIControlStateNormal)];
            }
        }
        
        
        if (self.PassWordTF.text.length > 0 && self.loginType == 0) {
            self.ShowPassWordButton.hidden = NO;
        }else{
            self.ShowPassWordButton.hidden = YES;
        }
        
        
    });
    
    
    return YES;
}
- (UIView *)SetNoteLogin{
    self.CodeTF = [[UITextField alloc] init];
    self.CodeTF.placeholder = @"请输入验证码";
    self.CodeTF.textColor = rgb(40, 40, 40);
    [self.CodeTF setValue:rgb(152, 152, 152) forKeyPath:@"_placeholderLabel.textColor"];
    
    [self.CodeTF  setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    
    UIView * view = [[UIView alloc] init];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.PassWordTF);
        make.right.equalTo(self).offset(0);
     }];
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    
    [btn setTitleColor:rgb(152, 152, 152) forState:(UIControlStateNormal)];
    [view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(view);
        make.width.mas_equalTo(120);
        make.right.mas_equalTo(-10);
    }];
    btn.titleLabel.isAdaptiveFont = @"";
    btn.titleLabel.font = LBFont(14);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.CodeTF.placeholder = @"请输入验证码";
     self.CodeTF.keyboardType = UIKeyboardTypePhonePad;
    [view addSubview: self.CodeTF];
    [self.CodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(view);
        make.right.equalTo(btn.mas_left);
    }];
    [btn addTarget:self action:@selector(getCode:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView * lineView4 = [[UIView alloc] init];
    lineView4.backgroundColor = [UIColor colorWithHexString:@"#7d7a80"];
    [view addSubview:lineView4];
  
    [lineView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.equalTo(btn);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(1);
    }];
    return view;
}

- (void)LoginAction:(LoginBlock)block{
    self.block = block;
    
    [self endEditing:YES];
    if (self.PassWordTF.text.length > 0 && self.loginType == 2 ) {
        self.loginType = 0;
    }else if (self.CodeTF.text.length > 0  && self.loginType == 2){
        self.loginType = 1;
    }
    if(self.loginType != 2){
        if (self.PhoneNumTF.text.length==0||self.PhoneNumTF.text==nil) {
            [MBProgressHUD showSuccess:@"手机号为不能空" toView:self];
            return;
        }
    if (![self.PhoneNumTF.text isMobile]) {
        
            [MBProgressHUD showSuccess:@"手机号格式不正确" toView:self];
            return;
        }
        if (self.CodeTF.text.length == 0 && self.loginType == 1) {

            [MBProgressHUD showSuccess:@"请输入验证码" toView:self];
            return;
        }
        if (self.PassWordTF.text.length == 0 && self.loginType == 0) {
           
            
            if (self.PassWordTF.text.length==0) {
                [MBProgressHUD showSuccess:@"密码不能为空" toView:self];
                return;
            }
//            if (self.PassWordTF.text.length<6&&self.loginType == 0) {
//                [MBProgressHUD showSuccess:@"请输入6~12密码" toView:self];
//                return;
//            }
//            if (self.PassWordTF.text.length>12&&self.loginType == 0) {
//                [MBProgressHUD showSuccess:@"请输入6~12密码" toView:self];
//                return;
//            }
        }
    }
    NSString *urlString;
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
  
    if (self.loginType == 0) {

//        if (self.PassWordTF.text.length<6) {
//            [MBProgressHUD showSuccess:@"请输入6~12密码" toView:self];
//            return;
//        }
//        if (self.PassWordTF.text.length>12) {
//            [MBProgressHUD showSuccess:@"请输入6~12密码" toView:self];
//            return;
//        }
        NSString *tel = self.PhoneNumTF.text;
        NSString *password = self.PassWordTF.text;
        [dict setObject:tel forKey:@"tel"];
        [dict setObject:password forKey:@"password"];
        urlString = WKAccountLogin_WKURL;
    }else if (self.loginType == 1){
        NSString *tel = self.PhoneNumTF.text;
        NSString *code = self.CodeTF.text;
        [dict setObject:tel forKey:@"tel"];
        [dict setObject:code forKey:@"code"];
        
        urlString = WKVeritifyCodeLogin_WKURL;

    }else if (self.loginType == 2){
        
//            dict[@"wxkey"] = self.openID;
        }
//        dict[@"uuid"] = [self getUUID];

    [WYHXNetWorkManager post:urlString params:dict completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];


        if ([code integerValue]==200) {
            if (block) {
//                NSLog(@"%@",responseObj);
                NSString *token = responseObj[@"data"];
                NSDictionary *dict = (NSDictionary *)responseObj;
                NSDictionary *data = dict[@"data"];
                UserModel *model = [UserModel mj_objectWithKeyValues:data];
                [UserModel SaveUserModel:model];
                NSString *userMoney = data[@"userMoney"];
                [[NSUserDefaults standardUserDefaults] setObject:userMoney forKey:@"userMoney"];
                [[NSUserDefaults standardUserDefaults] synchronize];
//                NSLog(@"%@",[UserModel getUserModel]);
                
                block(YES);
            }
        }else{ //if ([code integerValue]==500){
            NSString *msg = responseObj[@"msg"];
            [MBProgressHUD showSuccess:msg toView:self];
            return ;
        }
    }];

}

- (void)WeChatLoginAction{
    UIViewController * vc = [self getCurrentVC];
    self.PhoneNumTF.text = @"";
    self.PassWordTF.text = @"";
    self.CodeTF.text = @"";
    self.loginType = 2;

    [MBProgressHUD showSuccess:@"正在获取微信信息" toView:self];
    NSString *token = [UserModel getUserModel].token;
    if (token) {
        
    }else{
        [[WXAuth sharedInstance] loginAuthWithBlock:^(BOOL isOK, NSString *openID, NSString *unionID, NSString *userName, NSString *userAvatar, id data) {
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            //将微信的json 串转换成 字符串 传给后台
            if (!data) {
                return ;
            }
            NSError *parseError = nil;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&parseError];
            NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            [MBProgressHUD hideHUDForView:self];
            if (isOK) {
                [param setObject:json forKey:@"chatJson"];
                [WYHXNetWorkManager post:WKweixinShouquan_WKURL params:param completionHandler:^(id responseObj, NSError *error) {
                    NSString *code = responseObj[@"code"];
                    
//                    NSLog(@"%@",responseObj);
                    NSString *token = responseObj[@"data"][@"token"];
                    if ([code integerValue]==200) {

                        id object = [self nextResponder];
                        while (![object isKindOfClass:[UIViewController class]] && object != nil) {
                            object = [object nextResponder];
                        }
                        if ([token isEqual:[NSNull null]]||[token isKindOfClass:[NSNull class]] || token==nil) {

                            [MBProgressHUD showSuccess:@"微信授权成功" toView:self];
                            LBBoundingIponeVC *bonudingVC = [LBBoundingIponeVC new];
                            bonudingVC.OpenID = openID;
                            
                            UIViewController *superController = (UIViewController*)object;
                            
                            [superController.navigationController pushViewController:bonudingVC animated:YES];
                        }else{
                            NSDictionary *dict = (NSDictionary *)responseObj;
                            NSDictionary *data = dict[@"data"];
                            UserModel *model = [UserModel mj_objectWithKeyValues:data];
                            [UserModel SaveUserModel:model];
                            UIViewController *superController = (UIViewController*)object;
                            [superController.navigationController popToRootViewControllerAnimated:YES];
                        }
                    }
                }];
                
            }else{
                
//                [MBProgressHUD showSuccess:@"暂未开通此功能" toView:self];
//                return;
            }
        }];
    }

    
}
//- (void)GoHome:{
//    WKweixinShouquan_WKURL
//}
- (NSString *)getUUID {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
    NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    return result;
}

- (void)getCode:(UIButton *)btn{
    // 获取验证码
    if (![self.PhoneNumTF.text isMobile]) {
        [MBProgressHUD showSuccess:@"请输入正确的手机号" toView:self];
        return;
    }
    [self.CodeTF becomeFirstResponder];
    NSString *iphoneNum = self.PhoneNumTF.text;
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setObject:iphoneNum forKey:@"tel"];
    [WYHXNetWorkManager get:WKGetCode_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@%@",responseObj,error);
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
            // 开启倒计时
            [self openCountdown:btn];
        }

    }];
    return;
    
}

// 开启倒计时效果
-(void)openCountdown:(UIButton *)btn{
    
    btn.userInteractionEnabled = NO;
  
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [btn setTitle:@"重新发送" forState:UIControlStateNormal];
             
                btn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [btn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
             
                btn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

@end
